package com.the_ring.chapter12.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

/**
 * ObjectAnimation anim = ObjectAnimation.ofInt(sv_shutter, "ratio", 0, 100);
 * anim.setDuration(3000);
 * anim.start();
 */
public class ShutterView extends View {

    private Paint mPaint = new Paint();
    private int mOrientation = LinearLayout.HORIZONTAL;
    private int mLeafCount = 10;
    private PorterDuff.Mode mMode = PorterDuff.Mode.DST_IN; // 只展示交集
    private Bitmap mBitmap;
    private int mRatio = 0;

    private boolean initTag = true;     // 首次绘制
    private RectF rectF; // 图片绘制的坐标


    public ShutterView(Context context) {
        this(context, null);
    }

    public ShutterView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (mBitmap == null) {
            return;
        }
        int width = getMeasuredWidth();
        int height = getMeasuredHeight();
        // 创建一遮罩层
        Bitmap mask = Bitmap.createBitmap(width, height, mBitmap.getConfig());
        Canvas canvasMask = new Canvas(mask);
        for (int i = 0; i < mLeafCount; i++) {
            if (mOrientation == LinearLayout.HORIZONTAL) {
                int columnWidth = (int) Math.ceil(width * 1f / mLeafCount);
                int left = columnWidth * i;
                int right = left + columnWidth * mRatio / 100;
                // 在遮罩层上绘制各矩形叶片
                canvasMask.drawRect(left, 0, right, height, mPaint);
            } else {
                int rowHeight = (int) Math.ceil(height * 1f / mLeafCount);
                int top = rowHeight * i;
                int bottom = top + rowHeight * mRatio / 100;
                canvasMask.drawRect(0, top, width, bottom, mPaint);
            }
        }
        // 设置离层缓存
        int saveLayer = canvas.saveLayer(0, 0, width, height, null, Canvas.ALL_SAVE_FLAG);

        // 计算图片的显示位置
        if (initTag) {
            if (mBitmap.getWidth() / width > mBitmap.getHeight() / height) {
                int realWidth = width;
                int realHeight = width * mBitmap.getHeight() / mBitmap.getWidth();
                rectF = new RectF(0, (height - realHeight) / 2, width, (height + realHeight) / 2);
            } else {
                int realWidth = height * mBitmap.getWidth() / mBitmap.getHeight();
                int realHeight = height;
                rectF = new RectF((width - realWidth) / 2, 0, (width + realWidth) / 2, height);
            }
            initTag = false;
        }
        canvas.drawBitmap(mBitmap, null, rectF, mPaint);
        mPaint.setXfermode(new PorterDuffXfermode(mMode));
        canvas.drawBitmap(mask, 0, 0, mPaint);
        mPaint.setXfermode(null);
        canvas.restoreToCount(saveLayer);
    }

    public void setOrientation(int mOrientation) {
        this.mOrientation = mOrientation;
    }

    public void setLeafCount(int mLeafCount) {
        this.mLeafCount = mLeafCount;
    }

    public void setMode(PorterDuff.Mode mMode) {
        this.mMode = mMode;
    }

    public void setBitmap(Bitmap mBitmap) {
        this.mBitmap = mBitmap;
        initTag = true;
    }

    public void setRatio(int mRatio) {
        this.mRatio = mRatio;
        postInvalidate();
    }
}
